﻿using System;
using System.ComponentModel;
using System.Web.Mvc;
using FsCarInfo.Core.Entity;
using FsCarInfo.Facade;
using HMIS.FrameWork;
using HMIS.MVC.CommonSupport;
using HMIS.MVC.ViewModel;

namespace HMIS.MVC.Controllers
{
	public class CarController : BaseController
	{
		private readonly ICarFacade CarFacade;
		//private readonly IActionPermissionFacade ActionPermissionFacade;

		public CarController(ICarFacade CarFacade)
		{
			this.CarFacade = CarFacade;
			//this.ActionPermissionFacade = ActionPermissionFacade;
		}

		[Description("获取整车列表")]
		public ActionResult GetPlist()
		{
			long total;
			var data = CarFacade.GetPlist(
				int.Parse(Request["start"]),
				int.Parse(Request["limit"]),
				Request["sort"],
				Request["dir"],
				this.DesDataFilter(Request["filter"]),
				out total);
			return this.JsonFormat(
				new ExtGirdData<Car> { total = total, data = data },
				new string[] { "CustomType" });
		}

		[Description("获取整车列表")]
		public ActionResult QueryCarList()
		{
			long total;
			var data = CarFacade.QueryCarList(
				Request["query"],
				int.Parse(Request["start"] == null ? "0" : Request["start"]),
				int.Parse(Request["limit"] == null ? "0" : Request["limit"]),
				Request["sort"],
				Request["dir"],
				this.DesDataFilter(Request["filter"]),
				out total);
			return this.JsonFormat(
				new ExtGirdData<Car> { total = total, data = data },
				new string[] { "CustomType" });
		} 

		[Description("访问整车管理页面")]
		[ViewPage]
		public ActionResult Car()
		{
			return View();
		}

		[ExtResult]
		[Description("添加修改整车信息")]
		public ActionResult SaveOrUpdate()
		{
			Car entity;
			if (String.IsNullOrEmpty(Request["Id"]))
			{
				entity = new Car
				{
					IDCardNo = Request["IDCardNo"],
					TDeport = Request["TDeport"],
					Brand = Request["Brand"],
					Model = Request["Model"],
					Color = Request["Color"],
					Engin_ID = Request["Engin_ID"],
					Frame_ID = Request["Frame_ID"],
					Prod_Date = Convert.ToDateTime(Request["Prod_Date"]),
					Miles = Convert.ToInt32(Request["Miles"]),
					Appraisal = Convert.ToDecimal(Request["Appraisal"]),
					Cost = Convert.ToDecimal(Request["Cost"]),
					VatID = Request["VatID"],
					CurrentDep = Request["CurrentDep"],
					Remark = Request["Remark"],
					//附件信息
					Atm_GJB = Convert.ToInt32(Request["Atm_GJB"]),
					Atm_BT = Convert.ToInt32(Request["Atm_BT"]),
					Atm_SMS = Convert.ToInt32(Request["Atm_SMS"]),
					Atm_DYQ = Convert.ToInt32(Request["Atm_DYQ"]),
					Atm_BXK = Convert.ToInt32(Request["Atm_BXK"]),
					Atm_KEY = Convert.ToInt32(Request["Atm_KEY"]),
					Atm_MAST = Convert.ToInt32(Request["Atm_MAST"]),
					Atm_WIPER = Convert.ToInt32(Request["Atm_WIPER"]),
					Atm_CERTIFICATE = Convert.ToInt32(Request["Atm_CERTIFICATE"]),
					//车主信息
					Sal_CUSTNA = Request["Sal_CUSTNA"],
					Sal_CUSTID = Request["Sal_CUSTID"],
					Sal_CUSTEL = Request["Sal_CUSTEL"],
					Sal_DATE = Request["Sal_Date"] == null ?new DateTime(1999, 1, 1, 0, 0, 0, 0) :Convert.ToDateTime(Request["Sal_DATE"])
				};
				CarFacade.SaveOrUpdate(entity);
			}
			else
			{
				entity = CarFacade.Get(Request["Id"]);
				entity.TDeport = Request["TDeport"];				
				entity.Brand = Request["Brand"];
				entity.Model = Request["Model"];
				entity.Color = Request["Color"];
				entity.Engin_ID = Request["Engin_ID"];
				entity.Frame_ID = Request["Frame_ID"];
				entity.Prod_Date = Convert.ToDateTime(Request["Prod_Date"]);
				entity.Miles = Convert.ToInt32(Request["Miles"]);
				entity.Appraisal = Convert.ToDecimal(Request["Appraisal"]);
				entity.Cost = Convert.ToDecimal(Request["Cost"]);
				entity.VatID = Request["VatID"];
				entity.CurrentDep = Request["CurrentDep"];
				entity.Remark = Request["Remark"];
				//附件信息
				entity.Atm_GJB = Convert.ToInt32(Request["Atm_GJB"]);
				entity.Atm_BT = Convert.ToInt32(Request["Atm_BT"]);
				entity.Atm_SMS = Convert.ToInt32(Request["Atm_SMS"]);
				entity.Atm_DYQ = Convert.ToInt32(Request["Atm_DYQ"]);
				entity.Atm_BXK = Convert.ToInt32(Request["Atm_BXK"]);
				entity.Atm_KEY = Convert.ToInt32(Request["Atm_KEY"]);
				entity.Atm_MAST = Convert.ToInt32(Request["Atm_MAST"]);
				entity.Atm_WIPER = Convert.ToInt32(Request["Atm_WIPER"]);
				entity.Atm_CERTIFICATE = Convert.ToInt32(Request["Atm_CERTIFICATE"]);
				//车主信息
				entity.Sal_CUSTNA = Request["Sal_CUSTNA"];
				entity.Sal_CUSTID = Request["Sal_CUSTID"];
				entity.Sal_CUSTEL = Request["Sal_CUSTEL"];
				//entity.Sal_DATE = Convert.ToDateTime(Request["Sal_DATE"]);
				//保存信息
				CarFacade.SaveOrUpdate(entity);
			}

			return this.JsonFormat(new ExtResult { success = true, id = entity.Id });
		}

		[Description("获取商品车实体")]
		public ActionResult GetCarById()
		{
			var entity = CarFacade.Get(Request["Id"]);
			return this.JsonFormat(new ExtFormLoadData<Car> { data = new Car[] { entity } });
		}

		[ExtResult]
		[Description("判断发动机号、车架号等是否在类别下唯一")]
		public ActionResult IsFieldExist()
		{
			var result = CarFacade.IsFieldExist(Request["FieldName"], Request[Request["FieldName"]], Request["Id"]);
			return this.JsonFormat(new ExtResult { success = !result, msg = "已存在相同的项!" });
		}

		[ExtResult]
		[Description("删除商品车基本资料")]
		public ActionResult Delete()
		{
			CarFacade.Delete(Request["Id"]);
			return this.JsonFormat((new ExtResult { success = true }));
		}
	}
}
